package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyAccountService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 * 账单服务-临时停车缴费到账操作队列消费者。.
 * </desc>
 *
 * @createDate 2017-10-25
 */
@Component
public class ParkingBillPayConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(ParkingBillPayConsumer.class); //日志对象

    @Autowired
    IPropertyAccountService iPropertyAccountService; //账单信息处理接口

    /**
     * <desc>
     * 账单服务-临时停车缴费到账消费过程.
     * </desc>
     *
     * @param message 监听到的业务消息
     * @param channel 频道
     * @author Qiang.S
     * @createDate 2018-11-08
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_PARKING_BILL_PEY_MANAGER_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO po= JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String,Object> map =  JsonUtil.toObject(po.getMessageContent(),HashMap.class);
            iPropertyAccountService.parkingBillPay(map.get("feeId").toString(),map.get("account").toString(),
                    map.get("projectId").toString(),map.get("transactionId").toString(),map.get("tradeId").toString(),map.get("realTotal").toString());
        } catch (Exception ex) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_账单服务-临时停车缴费到账消费端】:消费异常", ex);
        }
        finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
